package me.jiatao.googlebilling.billing;

import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.SkuDetails;

import java.util.List;

import androidx.annotation.NonNull;

public abstract class OnGoogleBillingListener {
    /**
     * 连接google服务成功
     */
    public void onSetupSuccess() {
    }

    /**
     * google服务断开
     */
    public void onBillingServiceDisconnected() {
    }

    /**
     * 商品查询成功
     *
     * @param skuType 内购或者订阅
     * @param list    商品列表
     */
    public void onQuerySuccess(@NonNull String skuType, @NonNull List<SkuDetails> list) {
    }

    /**
     * 购买成功
     *
     * @param purchase 商品
     */
    public void onPurchaseSuccess(@NonNull String skuType, @NonNull Purchase purchase) {
    }

    /**
     * 每次启动重新检查订单，返回已支付但未消耗的订单
     * 这里需要重新上报服务端完成补单操作
     *
     * @param skuType  内购或者订阅
     * @param purchase 商品
     */
    public void onRecheck(@NonNull String skuType, @NonNull Purchase purchase) {
    }

    /**
     * 消耗成功
     *
     * @param purchaseToken token
     */
    public void onConsumeSuccess(@NonNull String purchaseToken) {
    }


    /**
     * 确认购买成功
     */
    public void onAcknowledgePurchaseSuccess() {
    }

    /**
     * 支付流程中google返回的失败回调
     *
     * @param tag    {@link GoogleBillingUtil.GoogleBillingListenerTag}
     * @param result 返回码{https://developer.android.com/google/play/billing/billing_reference}
     */
    public void onFail(@NonNull GoogleBillingUtil.GoogleBillingListenerTag tag, BillingResult result) {

    }

    /**
     * google组件初始化失败
     *
     * @param tag {@link GoogleBillingUtil.GoogleBillingListenerTag}
     */
    public void onError(@NonNull GoogleBillingUtil.GoogleBillingListenerTag tag) {
    }


    public final String getErrorMsg(BillingResult result) {
        String failMessage = "";
        switch (result.getResponseCode()) {
            case BillingClient.BillingResponseCode.USER_CANCELED:
                //failMessage = ResourceUtil.getStringContent("caveman_pay_cancel");
                break;
            case BillingClient.BillingResponseCode.SERVICE_UNAVAILABLE:
                //failMessage = ResourceUtil.getStringContent("caveman_net_connect_time_out");
                break;
            case BillingClient.BillingResponseCode.BILLING_UNAVAILABLE:
                //failMessage = ResourceUtil.getStringContent("caveman_billing_unavailable");
                break;
            case BillingClient.BillingResponseCode.ITEM_UNAVAILABLE:
                //failMessage = ResourceUtil.getStringContent("caveman_item_unavailable");
                break;
            case BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED:
                //failMessage = ResourceUtil.getStringContent("caveman_item_already_owned");
                break;
            default:
                //failMessage = "code: " + result.getResponseCode();
                break;
        }

        return failMessage;
    }
}
